﻿using Developer.Core.FrameWork.GlobalVar;
using Developer.Core.FrameWork.HelpFile;
using Developer.Core.FrameWork.MiddlePlatformService.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Developer.Core.FrameWork.MiddlePlatformService.Service.Impl
{
    /// <summary>
    /// 中台服务http请求服务接口实现类
    /// </summary>
    public class HttpMiddlePlatformService:IMiddlePlatformService
    {
        private readonly IHttpClientFactory httpClientFactory;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="httpClientFactory"></param>
        public HttpMiddlePlatformService(IHttpClientFactory httpClientFactory)
        {
            this.httpClientFactory = httpClientFactory;
        }

        /// <summary>
        /// 响应结果转换
        /// </summary>
        /// <param name="httpResponseMessage"></param>
        /// <returns></returns>
        public MiddlePlatformResult GetMiddlePlatformResult(HttpResponseMessage httpResponseMessage)
        {
            if (httpResponseMessage.StatusCode.Equals(HttpStatusCode.OK) || httpResponseMessage.StatusCode.Equals(HttpStatusCode.Created)
                || httpResponseMessage.StatusCode.Equals(HttpStatusCode.Accepted))
            {
                string JsonStr = httpResponseMessage.Content.ReadAsStringAsync().Result;

                return MiddlePlatformResult.JsonToMiddlePlatformResult(JsonStr);
            }
            else
            {
                throw new Exception($"{GlobalVars.HttpName}服务调用错误：{httpResponseMessage.Content.ReadAsStringAsync().ToString()}");
            }
        }

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Get(string middleUrl, IDictionary<string, object> middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换为url方式
            string urlParam = HttpParamUtil.DicToHttpUrlParam(middleParam);

            // 3、Get请求
            HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(middleUrl + urlParam);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Post(string middleUrl, IDictionary<string, object> middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParam), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Post请求，动态参数
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> PostDynamic(string middleUrl, dynamic middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParam), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParams"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Post(string middleUrl, IList<IDictionary<string, object>> middleParams)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParams), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Put(string middleUrl, IDictionary<string, object> middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParam), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Put请求，动态参数
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> PutDynamic(string middleUrl, dynamic middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParam), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Put(string middleUrl, IList<IDictionary<string, object>> middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、参数转换Json
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(middleParam), Encoding.UTF8, "application/json");

            // 3、Post请求
            HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(middleUrl, httpContent);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="middleUrl"></param>
        /// <param name="middleParam"></param>
        /// <returns></returns>
        public async Task<MiddlePlatformResult> Delete(string middleUrl, IDictionary<string, object> middleParam)
        {
            // 1、获取httpclient
            HttpClient httpClient = this.httpClientFactory.CreateClient(GlobalVars.HttpName);

            // 2、Delete请求
            HttpResponseMessage httpResponseMessage = await httpClient.DeleteAsync(middleUrl);

            return this.GetMiddlePlatformResult(httpResponseMessage);
        }

    }
}
